<template>
  <div class="performanceAssessAddOrModify-box content-box">
    <nav-title v-if="type=='add'" title="新增关键事件" :routerLink="routerLink"/>
    <nav-title v-else-if="type=='modify'" :title="title" :routerLink="routerLink"/>
    <div v-for="item in allInfo" class="performanceAssessAddOrModify-list">
      <div class="label">{{item.title}}</div>
      <div class="value" v-if="item.fileType == 'TEXT'">
        <input type="text" placeholder="请填写" style="width: 100%;" :disabled="!edit" maxlength="50" v-model="item.fieldValue"/>
      </div>
      <div class="value" v-else-if="item.fileType == 'DATE'">

      </div>
      <div class="value" v-else-if="item.fileType == 'SELECT'">

      </div>
    </div>
    <!-- <div class="performanceAssessAddOrModify-list" v-if="type=='modify'">
      <div class="label" style="color: #000; fontWeight: bold;">加减分</div>
      <div class="value"><input type="text" placeholder="请填写" style="width: 100%;"></div>
    </div> -->
    <div class="btn-box" v-if="edit">
      <button @click="saveOrDelete('save')">保存</button>
      <button v-if="type=='add'" @click="()=>{this.$router.go(-1);}">取消</button>
      <button v-else="" @click="saveOrDelete('delete')">删除</button>
    </div>
    <div v-else="" class="return-btn">
      <button @click="()=>{this.$router.go(-1);}">返回</button>
    </div>
  </div>
</template>

<script>
import {
  perassessApproveAddFields,
  perassessApproveSaveassess,
  perassessApproveSaveitem,
  perassessApproveDelitem
} from "../../api/api";
import { Group, PopupRadio } from "vux";
import qs from "qs";

export default {
  name: "performanceAssessAddOrModify",
  components: {
    Group,
    PopupRadio
  },
  created() {
    this.cTransactionNbr = this.$route.query.cTransactionNbr;
    this.docid = this.$route.query.docid;
    this.version = this.$route.query.version;
    this.sectionId = this.$route.query.sectionId;
    this.catType = this.$route.query.catType;
    this.itemId = this.$route.query.itemId;
    this.title = this.$route.query.title;
    this.seqnbr = this.$route.query.seqnbr;
    this.epPhase = this.$route.query.epPhase;
    this.type = this.$route.query.type;
    if (this.$route.query.edit == false || this.$route.query.edit == "false") {
      this.edit = false;
    } else {
      this.edit = true;
    }
    this.perassessApproveAddFieldsFunc();
  },
  mounted() {},
  data() {
    return {
      edit: true,
      type: "",
      routerLink: "",
      allInfo: {},
      title: "",
      cTransactionNbr: "",
      docid: "",
      version: "",
      sectionId: "",
      catType: "",
      itemId: "",
      data: []
    };
  },
  methods: {
    // getData() {
    //   console.debug(this.year);
    // },
    perassessApproveAddFieldsFunc() {
      this.$vux.loading.show();
      // let params = {};
      // if (this.type == "add") {
      let params = {
        cTransactionNbr: this.cTransactionNbr,
        docid: this.docid,
        version: this.version,
        sectionId: this.sectionId,
        catType: this.catType,
        seqnbr: this.seqnbr,
        epPhase: this.epPhase,
        itemId: this.itemId
      };
      // } else if (this.type == "modify") {
      //   params = {
      //     cTransactionNbr: this.cTransactionNbr,
      //     docid: this.docid,
      //     version: this.version,
      //     sectionId: this.sectionId,
      //     catType: this.catType,
      //     itemId: this.itemId
      //   };
      // }

      perassessApproveAddFields(params)
        .then(res => {
          this.$vux.loading.hide();
          if (res && res.retCode === "000000") {
            this.allInfo = res.retData;
          } else {
            this.$vux.alert.show({
              title: "提示",
              content: res.retMsg
            });
          }
        })
        .catch(res => {
          this.$vux.loading.hide();
          this.$vux.alert.show({
            title: "警告",
            content: "网络异常，请稍后重试"
          });
        });
    },
    saveOrDelete(action) {
      let title = "";
      let foo = "";
      let arr = [];
      if (action == "save") {
        title = "保存";
        foo = perassessApproveSaveitem;
        this.allInfo.map(item => {
          const obj = {
            fieldName: item.fieldName,
            fieldValue: item.fieldValue
          };
          arr.push(obj);
        });
        let hasErr = false;
        let errTitle = "";
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].fieldValue) {
            hasErr = true;
            this.allInfo.map(item => {
              if (item.fieldName == arr[i].fieldName) {
                errTitle = item.title;
              }
            });
            break;
          }
        }
        if (hasErr) {
          this.$vux.alert.show({
            title: "提示",
            content: `${errTitle}不能为空！`
          });
          return;
        }
      } else {
        title = "删除";
        foo = perassessApproveDelitem;
      }
      this.$vux.loading.show();
      const params = {
        cTransactionNbr: this.cTransactionNbr,
        docid: this.docid,
        version: this.version,
        sectionId: this.sectionId,
        catType: this.catType,
        seqnbr: this.seqnbr,
        epPhase: this.epPhase,
        itemId: this.itemId
      };
      if (action == "save") {
        params.itemJson = JSON.stringify(arr);
      }
      foo(qs.stringify(params))
        .then(res => {
          this.$vux.loading.hide();
          if (res && res.retCode === "000000") {
            this.$vux.alert.show({
              title: "成功",
              content: `${title}成功！`,
              onHide: () => {
                this.$router.go(-1);
              }
            });
          } else {
            this.$vux.alert.show({
              title: "提示",
              content: res.retMsg
            });
          }
        })
        .catch(res => {
          this.$vux.loading.hide();
          this.$vux.alert.show({
            title: "警告",
            content: "网络异常，请稍后重试"
          });
        });
    }
  }
};
</script>

<style type="text/scss" lang="scss">
.performanceAssessAddOrModify-box {
  padding-bottom: 50px;
  .performanceAssessAddOrModify-list {
    display: flex;
    text-align: left;
    padding: 0 10px;
    .label,
    .value {
      padding: 10px 0;
      border-bottom: 1px solid #ccc;
    }
    .label {
      flex: 2;
      color: #999;
    }
    .value {
      flex: 3;
    }
    .arrow.value {
      .weui-cell {
        padding: 0;
      }
      .weui-cell__ft {
        position: relative;
        width: 100%;
        text-align: left;
        &::after {
          content: none;
        }
        &::before {
          content: "";
          position: absolute;
          width: 20px;
          height: 8px;
          background: url("../vacation/images/u183.png") 0 0 no-repeat;
          top: 8px;
          right: 20px;
          background-size: 20px 8px;
        }
      }
    }
  }
  .btn-box {
    padding: 20px;
    margin-bottom: 20px;
    button {
      padding: 10px 30px;
      font-size: 16px;
      border-radius: 5px;
      color: #fff;
      font-weight: bold;
      margin: 0 20px;
      &:first-child {
        background-color: rgba(28, 170, 254, 1);
      }
      &:last-child {
        background-color: rgba(255, 152, 0, 1);
      }
    }
  }
  .return-btn > button {
    width: 90%;
    height: 40px;
    line-height: 40px;
    border-radius: 8px;
    font-size: 18px;
    color: #000;
    outline: none;
    margin-bottom: 50px;
    background-color: #fff;
    font-weight: bold;
    box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.349019607843137);
    margin-top: 30px;
    border: 1px solid #ccc;
  }
}
</style>
